﻿package Cain.editor
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	/**
	 * 物件編輯 等比放大縮小 旋轉 v1.0
	 * @author Cain@2009 in ADCAST
	 */
	public class Sc_editer
	{
		private var mc:Sprite;
		private var sp:Sprite;
		private var bb:Sprite;
		private var d:Number;
		private var r:Number;
		private var fn:Sprite;
		/**
		 * 要編輯的物件
		 * @param	_mc
		 */
		public function Sc_editer(_mc:Sprite) 
		{
			mc = _mc;
			init();
		}
		
		private function init():void
		{
			si_it();//裝入容器sp
			bb_init();//新增編輯點
		}
		
		private function bb_init():void
		{
			var mc_ed_point:Point = get_mc_ed_point();//取得mc左下角座標
			bb = new BB();
			bb.name = "sc_editer_bb_point";
			bb.x = mc_ed_point.x;
			bb.y = mc_ed_point.y;
			sp.addChild(bb);
			bb.addEventListener(MouseEvent.MOUSE_DOWN, bb_mousedown);
			d = new Point(bb.x, bb.y).length; //一倍距離;
			r = Math.acos(Math.abs(bb.y) / d) * 180 / Math.PI;
			bb_ud();
		}
		/**
		 * 物件
		 */
		public function get _mc():Sprite 
		{
			return mc;
		}
		private function fn_init():void
		{
			fn = new Sprite();
			fn.graphics.lineStyle(1, 0x000000, 1, false, "none");
			fn.graphics.moveTo(mc.getRect(mc).x, mc.getRect(mc).y);
			fn.graphics.lineTo(mc.getRect(mc).x + mc.width, mc.getRect(mc).y);
			fn.graphics.lineTo(mc.getRect(mc).x + mc.width, mc.getRect(mc).y + mc.height);
			fn.graphics.lineTo(mc.getRect(mc).x , mc.getRect(mc).y + mc.height);
			fn.graphics.lineTo(mc.getRect(mc).x , mc.getRect(mc).y);
			fn.graphics.endFill();
			mc.addChild(fn);
			fn.visible = false;
		}
		/**
		 * 開始編輯
		 */
		public function bb_ed():void 
		{
			fn.visible = true;
			bb.visible = true;
			mc.addEventListener(MouseEvent.MOUSE_DOWN, mc_mousedown);
			mc_mousedown();
		}
		
		private function del_on(e:KeyboardEvent):void 
		{
			if (e.keyCode==8 || e.keyCode==46) 
			{
				bb_ud();
				unload();
			}
		}
		/**
		 * 取消編輯
		 */
		public function bb_ud():void 
		{
			fn.visible = false;
			bb.visible = false;
			mc.removeEventListener(MouseEvent.MOUSE_DOWN, mc_mousedown);
		}
		private function mc_mousedown(e:MouseEvent=null):void 
		{
			sp.startDrag();
			mc.addEventListener(MouseEvent.MOUSE_UP, mc_mouseup);
		}
		
		private function mc_mouseup(e:MouseEvent):void 
		{
			sp.stopDrag();
			mc.removeEventListener(MouseEvent.MOUSE_UP, mc_mouseup);
		}
		/**
		 * 容器
		 */
		public function get _sp():Sprite 
		{
			return sp;
		}
		private function bb_mousedown(e:MouseEvent = null):void //bb按下
		{
			bb.startDrag();
			bb.addEventListener(MouseEvent.MOUSE_UP, bb_mouseup);
			bb.addEventListener(Event.ENTER_FRAME, bb_play);
			bb.addEventListener(MouseEvent.MOUSE_OUT, bb_mouseup);
		}
		
		private function bb_play(e:Event = null):void //mc隨著bb變動
		{
			var now_d:Number = new Point(bb.x, bb.y).length;
			var sc:Number = now_d / d;
			mc.scaleX = mc.scaleY = sc;
			var now_r:Number = Math.acos(Math.abs(bb.y) / now_d) * 180 / Math.PI;
				if (bb.y < 0 && bb.x > 0) {
					//第一象限
					mc.rotation = -((90 - r)+(90-now_r));
				}else if (bb.x<0 && bb.y<0)
				{
					//第二象限
					mc.rotation = -(90 + (90-r)) -now_r;
				}else if (bb.x<0 && bb.y>0)
				{
					//第三象限
					mc.rotation = r + now_r;
				}else
				{
					//第四象限
					mc.rotation = r - now_r;
				}
			
		}
		
		private function bb_mouseup(e:MouseEvent):void //bb拿起
		{
			bb.removeEventListener(Event.ENTER_FRAME, bb_play);
			bb.removeEventListener(MouseEvent.MOUSE_UP, bb_mouseup);
			bb.removeEventListener(MouseEvent.MOUSE_OUT, bb_mouseup);
			bb.stopDrag();
			bb_play();
		}	
		private function get_mc_ed_point():Point
		{
			var point:Point = new Point(mc.getRect(mc).x + mc.width, mc.getRect(mc).y + mc.height);
			return point;
		}
		private function si_it():void 
		{
			sp = new Sprite();
			Sprite(mc.parent).addChild(sp);
			sp.addChild(mc);
			sp.x = mc.x;
			sp.y = mc.y;
			mc.x = 0;
			mc.y = 0;
			sp.name = mc.name;
			fn_init();//加入向量框
		}
		/**
		 * 移除編輯
		 */
		public function unload():void
		{
			bb.removeEventListener(MouseEvent.MOUSE_DOWN, bb_mousedown);
			sp.parent.removeChild(sp);
			delete this;
		}
	}
	
}